package com.tuananh.radnom.gwt.client.overall;

import java.util.ArrayList;

import com.chap.links.client.Network;
import com.chap.links.client.events.SelectHandler;
import com.chap.links.client.events.SelectHandler.SelectEvent;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.visualization.client.DataTable;
import com.google.gwt.visualization.client.Selection;
import com.google.gwt.visualization.client.VisualizationUtils;
import com.sencha.gxt.core.client.dom.ScrollSupport.ScrollMode;
import com.sencha.gxt.widget.core.client.container.FlowLayoutContainer;
import com.tuananh.radnom.gwt.shared.dto.NetConnDTO;
import com.tuananh.radnom.gwt.shared.dto.NetInterfaceDTO;
import com.tuananh.radnom.gwt.shared.dto.NetNodeDTO;

public class NetworkCanvas implements IsWidget {
	interface ImageBundle extends ClientBundle {
		@Source("computer.png")
		ImageResource computer();
	}

	interface MyUiBinder extends UiBinder<Widget, NetworkCanvas> {
	}

	private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);
	ArrayList<NetNodeDTO> listNode;
	ArrayList<NetConnDTO> listConn;
	@UiField
	FlowLayoutContainer con;

	Network network = null;

	public void updateStore(final ArrayList<NetNodeDTO> listNode,ArrayList<NetConnDTO> listConn) {
		if (this.listNode==null) this.listNode = new ArrayList<NetNodeDTO>();
		if (this.listConn==null) this.listConn = new ArrayList<NetConnDTO>();
		if (listNode != null) {
			this.listNode.clear();
			this.listNode.addAll(listNode);
			this.listConn.clear();
			this.listConn.addAll(listConn);
			
			VisualizationUtils.loadVisualizationApi(new Runnable() {

				public void run() {
					// TODO Auto-generated method stub
					DataTable nodes = DataTable.create();
					nodes.addColumn(DataTable.ColumnType.STRING, "id");
					nodes.addColumn(DataTable.ColumnType.STRING, "text");
					nodes.addColumn(DataTable.ColumnType.STRING,"style");
					nodes.addColumn(DataTable.ColumnType.STRING,"image");
					nodes.addColumn(DataTable.ColumnType.STRING, "title");
					nodes.addColumn(DataTable.ColumnType.STRING,"tuananh");
					int i = -1;
					for (NetNodeDTO netNode : NetworkCanvas.this.listNode) {
						for (NetInterfaceDTO netInt : netNode
								.getNetInterfaces()) {
							i++;
							nodes.addRow();
							nodes.setValue(i, 0, netInt.getKey());
							nodes.setValue(i, 1, netInt.getName());
							nodes.setValue(i, 2, "image");
							nodes.setValue(i, 3, imageSource.computer().getSafeUri().asString());
							nodes.setValue(i, 4, netInt.getDisplayName());
							nodes.setValue(i, 5, "TuanAnh");
						}
					}
					
					// Create links table with some data
					DataTable links = DataTable.create();
					links.addColumn(DataTable.ColumnType.STRING, "from");
		            links.addColumn(DataTable.ColumnType.STRING, "to");
		            int linkRow=-1;
					for (NetConnDTO netConn:NetworkCanvas.this.listConn){
						String from ="", to="";
						for (i=0;i<nodes.getNumberOfRows();i++){
							System.out.println(nodes.getValueString(i, 0));
							if (nodes.getValueString(i, 0).equals(netConn.getParent())){
								from = netConn.getParent();
							}
							if (nodes.getValueString(i, 0).equals(netConn.getChild()))
								to= netConn.getChild();
						}
						if (!from.equals("")&& !to.equals("")){
							links.addRow();
							linkRow++;
							links.setValue(linkRow, 0, from);
							links.setValue(linkRow, 1, to);
						}
					}
					
					// Create options
					Network.Options options = Network.Options.create();
					options.setWidth(con.getParent().getOffsetWidth() + "px");
					options.setHeight(con.getParent().getOffsetHeight() + "px");

					network = new Network(nodes, links, options);
					
					con.add(network);
				}
			});
		}
	};

	ImageBundle imageSource = GWT.create(ImageBundle.class);

	public Widget asWidget() {
		// TODO Auto-generated method stub
		Widget widget = uiBinder.createAndBindUi(this);
		
		con.setScrollMode(ScrollMode.AUTO);
		return widget;
	}
}
